﻿using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.Text;
using Umi.Crypto.Abstraction.HMac;
using Umi.Crypto.Abstraction.Keys;
using Umi.Web.Metadatas.Attributes;

namespace Umi.Web.Crypto.HMacs
{
    [Service("HmacPassword", Scoped = ServiceScoped.Single)]
    public class HMacPasswordService : IPasswordService
    {
        public string Name => "HMac";

        public byte[] GenAuthCode(byte[] passwrod, KeyParameter key, string hash)
        {
            if (!(key is HMacKeyParameter))
                throw new NotSupportedException("不支持该Key");
            HMacKeyParameter hMacKey = (HMacKeyParameter)key;
            IDigest digest = DigestUtilities.GetDigest(hash);
            IMac mac = new HMac(digest);
            mac.Init(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(hMacKey.Key));
            mac.BlockUpdate(passwrod, 0, passwrod.Length);
            byte[] output = new byte[mac.GetMacSize()];
            mac.DoFinal(output, 0);
            return output;
        }
    }
}
